package com.zjun.demo.experimentaldemo.dynamicAdd

import android.icu.util.TimeZone
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintSet
import androidx.constraintlayout.widget.Guideline
import com.zjun.demo.experimentaldemo.R
import com.zjun.demo.experimentaldemo.base.BaseActivity
import com.zjun.demo.experimentaldemo.databinding.ActivityDynamicAddBinding

/**
 * 约束布局动态添加View
 */
class DynamicAddActivity : BaseActivity<ActivityDynamicAddBinding>() {

    override fun getLayoutId(): Int = R.layout.activity_dynamic_add

    override fun initialize() {
        val ivLeft = ImageView(this)
        ivLeft.id = R.id.ivLeft
        ivLeft.setImageResource(R.drawable.svg9)
        ivLeft.layoutParams = ConstraintLayout.LayoutParams(
            ConstraintLayout.LayoutParams.WRAP_CONTENT,
            DisplayUtil.dip2px(50f).toInt(),
        )
        ivLeft.setPadding(
            DisplayUtil.dip2px(15f).toInt(),
            DisplayUtil.dip2px(12f).toInt(),
            DisplayUtil.dip2px(30f).toInt(),
            DisplayUtil.dip2px(12f).toInt()
        )
        mBinding.container.addView(ivLeft)

        val constraintSetA = ConstraintSet()
        constraintSetA.clone(mBinding.container)
        constraintSetA.connect(ivLeft.id, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP)
        constraintSetA.connect(ivLeft.id, ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START)
        constraintSetA.connect(ivLeft.id, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM)
        constraintSetA.applyTo(mBinding.container)
    }

}